# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import requests
import os
import datetime

url = 'https://vod8eu16.128100.xyz/hls/rfcQapnC6Hs/index.m3u8?token=OE01YUJzb1JqbjE2cUFPbWNWWEhkZkg0Q0ZJL2ZsTFVvNktXaGlIOW4xNkZRc1hySHc0T0F1eVhCTm95aHI0OEJLWTJ4aWVlNmlLTVA5ZjU3emFWOFVyV2c5cHNpTTdkU3Y4dnYzMjFtZXpNemJTdkF0ZTNQQjAwTmQ1MVRzclE=&ip=47.242.233.182&auth=b367093bceee7ac511d4a5f75f46e708&exp=1609590863&hash=5cc50dadb24688e94655346601266dde&uri=L2hscy9yZmNRYXBuQzZIcy8=&pf=vod8-eu16&'
is_full_new = True
continue_number = 181

first = url.split('//')[1]
second = first.split('/')
host = second[0]
file_segment = '/' + second[1] + '/' + second[2] + '/'
last_segment = second[3]
path_prefix = file_segment
path = path_prefix + last_segment
proxies = {
    "https": "127.0.0.1:53934",
}


def single_segment(uri_str, i, folder_path):
    headers = {
        'authority': host,
        'method': 'get',
        'path': path_prefix + uri_str,
        'scheme': 'https',
        'accept': '*/*',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'origin': 'https://cableav.tv',
        'referer': 'https://cableav.tv/',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'cross-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    }
    url = 'https://' + host + path_prefix + uri_str
    r = requests.get(url=url, headers=headers, proxies=proxies)
    print(uri_str, r.status_code)
    if r.status_code == 200:
        f = open(folder_path + "/%06d.ts" % i, "wb")
        f.write(r.content)
        f.close()


def main_index():
    url = 'https://' + host + path
    headers = {
        'authority': host,
        'method': 'get',
        'path': path,
        'scheme': 'https',
        'accept': '*/*',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'origin': 'https://cableav.tv',
        'referer': 'https://cableav.tv/',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'cross-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    }
    r = requests.get(url=url, headers=headers, proxies=proxies)
    print(r.status_code)
    if r.status_code >200:
        return
    segmeng_list = r.text.split(',')
    k = 0
    time_stamp = datetime.datetime.now()
    time_stamp.strftime('%Y_%m_%d_%H_%M_%S')
    folder = time_stamp.strftime('%Y_%m_%d_%H_%M_%S')
    if not os.path.exists(folder):  # 判断是否存在文件夹如果不存在则创建为文件夹
        os.makedirs(time_stamp.strftime('%Y_%m_%d_%H_%M_%S'))
    for i in segmeng_list:
        uri_pfrefix = i.split('#')[0]
        uri = uri_pfrefix.replace('\n', '')

        if is_full_new:
            k = k + 1
            single_segment(uri, k, folder)
        else:
            if 'CLS' in uri:
                numbers = uri.split('-')
                if int(numbers[1]) > continue_number:
                    k = int(numbers[1])
                    single_segment(uri, k, folder)
                else:
                    print(uri)


if __name__ == '__main__':
    main_index()
